﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SdmxMl.Helper
{
    public class NumHelper
    {
        public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
        {
            Double result;
            return Double.TryParse(val, NumberStyle,
                System.Globalization.CultureInfo.CurrentCulture, out result);
        }

        public static bool IsListNumeric(List<string> list, System.Globalization.NumberStyles NumberStyle)
        {
            foreach (string s in list)
            {
                // if zero are prepended consider as ASCII
                if (s.Length > 1 && s[0] == '0')
                    return false;
                if (isNumeric(s, NumberStyle) == false)
                    return false;
            }
            return true;
        }

        public static byte[] HexToByte(string s)
        {
            byte[] b = new byte[s.Length / 2];
            for (int i = 0; i < b.Length; i++)
                b[i] = (byte)int.Parse(s.Substring(2 * i, 2), System.Globalization.NumberStyles.HexNumber);
            return b;
        }

        public static string ByteAsHex(byte[] b)
        {
            StringBuilder sb = new StringBuilder(200);
            for (int i = 0; i < b.Length; i++)
                sb.Append(string.Format("{0:X2}", b[i]));
            return sb.ToString();
        }

        // C# to convert a string to a byte array.
        public static byte[] StrToByteArray(string str)
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            return encoding.GetBytes(str);
        }

    }
}
